﻿using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using MockupDesigner.AppItems;
using System.Windows;
using MockupDesigner.AppItems.Commands;
using System.Windows.Shapes;

namespace MockupDesigner.AppItems.Controls
{
    public class DragThumb : Thumb
    {
        public DragThumb()
        {
            base.DragDelta += new DragDeltaEventHandler(DragThumb_DragDelta);
            base.DragStarted += new DragStartedEventHandler(DragThumb_DragStarted);
            base.DragCompleted += new DragCompletedEventHandler(DragThumb_DragCompleted);
        }

        private DragCommand dragCommand;

        void DragThumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            DesignerItem di = (DesignerItem)this.DataContext;
            if (!(VisualTreeHelper.GetParent(di) is DesignerCanvas))
                return;

            dragCommand = new DragCommand();
            dragCommand.SetInitialPositions((DesignerCanvas)VisualTreeHelper.GetParent(di), di);
        }

        void DragThumb_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            if (e.HorizontalChange == 0 && e.VerticalChange == 0)
                return;
            DesignerItem di = (DesignerItem)this.DataContext;
            if (!(VisualTreeHelper.GetParent(di) is DesignerCanvas))
                return;
            
            dragCommand.SetFinalDelta(e.HorizontalChange, e.VerticalChange);
            DesignerCanvas dc = (DesignerCanvas)VisualTreeHelper.GetParent(di);
            dc.CommandProcessor.ExecuteCommand(dragCommand, dc, null);
        }

        void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            DesignerItem designerItem = this.DataContext as DesignerItem;
            if (!(VisualTreeHelper.GetParent(designerItem) is DesignerCanvas))
                return;
            
            DesignerCanvas designer = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;

            if (designer != null && designerItem.IsSelected)
            {
                
                
                double minLeft = double.MaxValue;
                double minTop = double.MaxValue;

                // we only move DesignerItems
                var designerItems = designer.SelectionService.CurrentSelection.OfType<DesignerItem>();

                var allElements = designer.Children.OfType<DesignerItem>();

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top = Canvas.GetTop(item);

                    minLeft = double.IsNaN(left) ? 0 : Math.Min(left, minLeft);
                    minTop = double.IsNaN(top) ? 0 : Math.Min(top, minTop);
                }


                double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
                double deltaVertical = Math.Max(-minTop, e.VerticalChange);

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top = Canvas.GetTop(item);

                    if (double.IsNaN(left)) left = 0;
                    if (double.IsNaN(top)) top = 0;

                    left = left + deltaHorizontal;
                    top = top + deltaVertical;

                    Canvas.SetLeft(item, left);
                    Canvas.SetTop(item, top);
                }

                designer.InvalidateMeasure();
                e.Handled = true;
            }
        }
    }

    public enum MagnetismType
    {
        Horizontal, Vertical
    }
}
